﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PLC_S7_TOOL;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace plc_Test1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            var ip = txtIp.Text;
            if (PLC_S7_TOOL.S7.Connect(ip))
            {
                ShowCustomMessageBox(this, "连接成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            PLC_S7_TOOL.S7.DisConnect();
        }

        #region Write
        private void btnSendInt_Click(object sender, EventArgs e)
        {
            try
            {
                if (int.TryParse(rBoxSend.Text, out int value))
                {
                    int oi = PLC_S7_TOOL.S7.WriteInt(txtSend.Text, value);
                    if (oi == 0)
                    {
                        ShowCustomMessageBox(this, "写入成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        ShowCustomMessageBox(this, "写入失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    ShowCustomMessageBox(this, "写入失败，请检查输入格式", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void btnSendByte_Click(object sender, EventArgs e)
        {
            try
            {
                if (byte.TryParse(rBoxSend.Text, out byte value))
                {
                    int oi = PLC_S7_TOOL.S7.WriteBtye(txtSend.Text, value);
                    if (oi == 0)
                    {
                        ShowCustomMessageBox(this, "写入成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        ShowCustomMessageBox(this, "写入失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    ShowCustomMessageBox(this, "写入失败，请检查输入格式", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void btnSendFloat_Click(object sender, EventArgs e)
        {
            try
            {
                if (float.TryParse(rBoxSend.Text, out float value))
                {
                    int oi = PLC_S7_TOOL.S7.WriteFloat(txtSend.Text, value);
                    if (oi == 0)
                    {
                        ShowCustomMessageBox(this, "写入成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        ShowCustomMessageBox(this, "写入失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    ShowCustomMessageBox(this, "写入失败，请检查输入格式", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnSendDouble_Click(object sender, EventArgs e)
        {
            try
            {
                if (double.TryParse(rBoxSend.Text, out double value))
                {
                    int oi = PLC_S7_TOOL.S7.WriteDouble(txtSend.Text, value);
                    if (oi == 0)
                    {
                        ShowCustomMessageBox(this, "写入成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        ShowCustomMessageBox(this, "写入失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    ShowCustomMessageBox(this, "写入失败，请检查输入格式", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnSendBool_Click(object sender, EventArgs e)
        {
            try
            {
                if (bool.TryParse(rBoxSend.Text, out bool value))
                {
                    int oi = PLC_S7_TOOL.S7.WriteBool(txtSend.Text, value);
                    if (oi == 0)
                    {
                        ShowCustomMessageBox(this, "写入成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        ShowCustomMessageBox(this, "写入失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    ShowCustomMessageBox(this, "写入失败，请检查输入格式", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void btnSendString_Click(object sender, EventArgs e)
        {
            try
            {
                int oi = PLC_S7_TOOL.S7.WriteString(txtSend.Text, rBoxSend.Text);
                if (oi == 0)
                {
                    ShowCustomMessageBox(this, "写入成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowCustomMessageBox(this, "写入失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region Read

        private void btnReadInt_Click(object sender, EventArgs e)
        {
            try
            {
                rBoxRead.Text += $"[{DateTime.Now}] [{txtRead.Text}] {PLC_S7_TOOL.S7.ReadInt(txtRead.Text).ToString()}";
                rBoxRead.Text += "\r\n";
                rBoxRead.SelectionStart = rBoxRead.Text.Length; //在文本框中的选定文本的起始位置
                rBoxRead.ScrollToCaret(); //将控件内容滚动到当前插入符号位置
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void btnReadByte_Click(object sender, EventArgs e)
        {
            try
            {
                rBoxRead.Text += $"[{DateTime.Now}] [{txtRead.Text}] {PLC_S7_TOOL.S7.ReadBtye(txtRead.Text).ToString()}";
                rBoxRead.Text += "\r\n";
                rBoxRead.SelectionStart = rBoxRead.Text.Length; //在文本框中的选定文本的起始位置
                rBoxRead.ScrollToCaret(); //将控件内容滚动到当前插入符号位置
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnReadFloat_Click(object sender, EventArgs e)
        {
            try
            {
                rBoxRead.Text += $"[{DateTime.Now}] [{txtRead.Text}] {PLC_S7_TOOL.S7.ReadFloat(txtRead.Text).ToString()}";
                rBoxRead.Text += "\r\n";
                rBoxRead.SelectionStart = rBoxRead.Text.Length; //在文本框中的选定文本的起始位置
                rBoxRead.ScrollToCaret(); //将控件内容滚动到当前插入符号位置
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnReadDouble_Click(object sender, EventArgs e)
        {
            try
            {
                rBoxRead.Text += $"[{DateTime.Now}] [{txtRead.Text}] {PLC_S7_TOOL.S7.ReadDouble(txtRead.Text).ToString()}";
                rBoxRead.Text += "\r\n";
                rBoxRead.SelectionStart = rBoxRead.Text.Length; //在文本框中的选定文本的起始位置
                rBoxRead.ScrollToCaret(); //将控件内容滚动到当前插入符号位置
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnReadBool_Click(object sender, EventArgs e)
        {
            try
            {
                rBoxRead.Text += $"[{DateTime.Now}] [{txtRead.Text}] {PLC_S7_TOOL.S7.ReadBool(txtRead.Text).ToString()}";
                rBoxRead.Text += "\r\n";
                rBoxRead.SelectionStart = rBoxRead.Text.Length; //在文本框中的选定文本的起始位置
                rBoxRead.ScrollToCaret(); //将控件内容滚动到当前插入符号位置
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnReadStr_Click(object sender, EventArgs e)
        {
            try
            {
                rBoxRead.Text += $"[{DateTime.Now}] [{txtRead.Text}] {PLC_S7_TOOL.S7.ReadString(txtRead.Text).ToString()}";
                rBoxRead.Text += "\r\n";
                rBoxRead.SelectionStart = rBoxRead.Text.Length; //在文本框中的选定文本的起始位置
                rBoxRead.ScrollToCaret(); //将控件内容滚动到当前插入符号位置
            }
            catch (Exception)
            {

                throw;
            }
        }



        #endregion

        Assembly assembly;
        Version version;
        private void Form2_Load(object sender, EventArgs e)
        {
            assembly = Assembly.GetExecutingAssembly();
            version = assembly.GetName().Version;
            lblVerison.Text = $"版本号：V{version}";
            txtIp.Text = "127.0.0.1";
            txtRead.Text = "DB10.0";
            txtSend.Text = "DB10.0";
            this.StartPosition = FormStartPosition.Manual; // 确保不使用自动位置设置
            this.Location = new Point(
                Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2,
                Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);
        }

        public static void ShowCustomMessageBox(Form owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            using (CustomMessageBox messageBox = new CustomMessageBox(text, caption, buttons, icon))
            {
                messageBox.StartPosition = FormStartPosition.CenterParent; // 设置位置为父窗体中心
                messageBox.ShowDialog(owner);
            }
        }
    }
}
